
var tdColor=document.getElementsByClassName('td-color');
var play=document.getElementsByClassName('play')[0];
var loses=document.getElementsByClassName('lose')[0];
var wins=document.getElementsByClassName('win')[0];
var reset=document.getElementsByClassName('reset')[0];

var array=[0,1,2,3,4,5,6,7,8];
var arrayDate=[[0,1,2],[0,3,6],[0,4,8],[2,4,6],[2,5,8],[8,4,0],[8,7,6],[6,3,0],[6,4,2],[1,4,7],[3,4,5]];
var arrayWhite=[];//白棋所占格下标集合
var arrayBlack=[];//黑棋所占格下标集合


var arraywhiteObject=[];
var arrayBlackObject=[];


var judgeEnd=true;

var len=tdColor.length;


for(var i=0;i<len;i++){


	(function(j){



		tdColor[j].onclick=function play(){
			//console.log(j);
			var index=array["j"];

		if(judge(j)){//是否可以下子
			if(judgeEnd){


				var white=document.createElement('div');
				tdColor[j].appendChild(white);
				white.style.width='28px';
				white.style.height='28px';
				white.style.borderRadius='50%';
				white.style.backgroundColor='#fff';
				white.style.border="1px solid #353433";
				arrayWhite.push(j);
				arraywhiteObject.push(white);

			del(j);//删除

			if(win(arrayWhite)){
				//  var len5=arraywhiteObject.length
				
				// for(var i=0;i<tdColor.length;i++){
				// 	//arraywhiteObject.splice(i,1);
				// 	tdColor[i].removeEventListener("click",play,false);
				// }
				judgeEnd=false;
				wins.style.display='block';

			}


			//console.log("当前数组的值:"+array);	
if(array.length>0&&judgeEnd){//没有空格时停止比赛

	var time=setTimeout(tdColor.onclick=function(){

		var random=Math.floor(Math.random()*array.length);//下标
		var index2=array[random];//值->对应单元标识
		var black=document.createElement('div');
		tdColor[index2].appendChild(black);
		black.style.width='28px';
		black.style.height='28px';
		black.style.borderRadius='50%';
		black.style.backgroundColor='#000';
		black.style.border="1px solid #353433";

		arrayBlack.push(index2);

		arrayBlackObject.push(black);
		
		del(index2);
		if(win(arrayBlack)){
			judgeEnd=false;
			loses.style.display='block';
		}



	},100);

}
}
}
}

}(i));


}

//删除与当前点击单元格标识想同的数组的下标

function del(x){
	for(var index1=0;index1<len;index1++){
		if(array[index1]==x){
			array.splice(index1,1);
		}
	}
}
		//判断当前空格是否被黑子占据
		function judge(y){
			var count=0;
			for(var index2=0;index2<len;index2++){
				if(y==array[index2]){
					return true;
				}else{
					count++;
				}

				if(count==len){
					return false;
				}
			}
		}


		//输赢判断
		function win(arr){
			var countjudge=0;
			var len2=arr.length;
			var len3=arrayDate.length;
			for(var index4=0;index4<len3;index4++){
				var len4=arrayDate[0].length;
				for(var index5=0;index5<len4;index5++){

					for(var index3=0;index3<len2;index3++){

						if(arr[index3]==arrayDate[index4][index5]){
							countjudge++;
							// console.log("arr[index3]="+arr[index3]+"    arrayDate[index4][index5]="+arrayDate[index4][index5])
							// console.log("countjudge的值:"+countjudge);

							if(countjudge==3){
								return true;  
							}
						}
					}
				}
				countjudge=0;
			}
			return false;
		}

		reset.onclick=function(){
			// for(var index=0;index<arraywhiteObject.length;index++){
			// 	arraywhiteObject[index].style.display='none';
			// }
			// for(var index2=0;index2<arrayBlackObject.length;index2++){
			// 	arrayBlackObject[index2].style.display='none';
			// }
			play.style.display='none';
			//wins.style.display='none';
			// arrayWhite.splice(0,arrayWhite.length);
		 //    arrayBlack.splice(0,arrayBlack.length);
			// judgeEnd=true;
			//table.style.display='none';
			//reset.style.display='none';
		}
















































































